package thread;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

public class Test {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        // 线程1
        MyThread myThread = new MyThread();
        myThread.start();

        // 线程2
        Thread thread1 = new Thread(() -> {
            try {
                Thread.sleep(3L);
                System.out.println("实现Runnable接口实现类对象...");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        thread1.start();

        // 线程3
        MyCallable mc = new MyCallable();
        FutureTask<String> ft = new FutureTask<>(mc);
        Thread thread2 = new Thread(ft);
//        String s = ft.get();// 注意：ft.get()是一个阻塞方法，必须在start()之后，程序会阻塞，等待ft的return
        thread2.start();
        String s = ft.get();
        System.out.println(s);

        // 线程4
        ExecutorService es = Executors.newFixedThreadPool(1);
        es.submit(() -> {
            System.out.println("使用线程池创建...");
        });
        es.shutdown();

        // 主线程
        Thread.sleep(1L);
        System.out.println("main线程...");
//        System.out.println(s);
    }
}
